﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;


namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Student --

        public List<StudentGraduation> GetListStudentGraduation(int majorId)
        {
            return DataContex != null && majorId > 0
                       ? DataContex.StudentGraduations.Where(s => s.Student.IdMajor == majorId).ToList()
                       : null;
        }      

        #endregion       

        #region -- Add Student Graduation --

        /// <summary>
        /// 
        /// </summary>
        /// <param name="student"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddStudentGraduationValidator(StudentGraduation student, out string message)
        {
            if (DataContex.StudentGraduations.Any(f => f.IdStudent == student.IdStudent))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, student.Id);
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddStudentGraduation(StudentGraduation student)
        {
            DataContex.StudentGraduations.AddObject(student);
            DataContex.SaveChanges();
        }

        #endregion

        #region -- Edit Student Graduation --


        public bool EditStudentGraduationValidator(StudentGraduation student, out string message)
        {
            var id = student.Id;

            if (DataContex.StudentGraduations.Any(m => m.IdStudent == student.Id && m.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, student.Id);
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        public bool EditStudentGraduation(StudentGraduation student)
        {
            var id = student.Id;
            var updateStudent = DataContex.StudentGraduations.SingleOrDefault(f => f.Id == id);

            if (updateStudent == null)
                return false;

            updateStudent.Project = student.Project;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Student --

        public bool DeleteStudentGraduationsValidator(IEnumerable<StudentGraduation> students, out string message)
        {
            const string intialize = "Sinh viên làm KLTN: ";
            var itemsCannotIdentify = (from student in students
                                       let id = student.Id
                                       where !DataContex.StudentGraduations.Any(m => m.Id == id)
                                       select student).Aggregate(intialize, (current, student) => current + (student.Id + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }

            var itemsInUse = (from student in students
                              let id = student.Id
                              where DataContex.StudentLecturers.Any(m => m.IdStudent == id)
                              select student).Aggregate(intialize, (current, student) => current + (student.Id + ", "));

            if (itemsInUse != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsInUseCanNotDelete, itemsInUse.TrimEnd(','));
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteStudentGraduations(List<StudentGraduation> students)
        {
            for (var i = students.Count - 1; i >= 0; i--)
            {
                var id = students[i].Id;
                var deleteStudent = DataContex.StudentGraduations.Single(m => m.Id == id);
                DataContex.StudentGraduations.DeleteObject(deleteStudent);
            }
            DataContex.SaveChanges();
        }

        #endregion      
    }
}